package com.buyantech.smweb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.excel.ExcelAVieData;
import com.buyantech.smweb.model.Alliance;
import com.buyantech.smweb.service.AllianceManager;
import com.buyantech.smweb.vo.query.AllianceQuery;
import com.google.gson.Gson;

/**
 * 联盟
 * @version 1.0
 * @date 2012-05-10
 */
@Controller
public class AllianceController extends BaseSpringController {
	private AllianceManager allianceManager;
	
	private final String LIST_ACTION = "redirect:/pages/Alliance/list.do";
	 
	public void setAllianceManager(AllianceManager allianceManager) {
		this.allianceManager = allianceManager;
	}

 	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	 
	/** 
	 * 执行搜索 
	 **/
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,AllianceQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex"))+1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile=request.getParameter("sortField");
		if(sortfile!=null&&!sortfile.equals(""))
		  query.setSortColumns(request.getParameter("sortField")+" "+request.getParameter("sortOrder"));
		
		Page page = this.allianceManager.searchPage(query);
		modelMap.put("data", page.getResult());
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}	
	
	 /** 
	 * 执行export 
	 **/
	public ModelAndView exportData(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView result = new ModelAndView(new ExcelAVieData());
		String type="";
		List<String>headers = new ArrayList<String>();
		headers.add("姓名");
		headers.add("时间");
		headers.add("类型");
		headers.add("积分"); 
		headers.add("金额"); 
		headers.add("状态");
 
		AllianceQuery query=new AllianceQuery();
		query.setPageSize(10000);
		Page page = this.allianceManager.searchPage(query);
		
		result.addObject("headers", headers);
		result.addObject("type", "1");
		result.addObject("content", page.getResult());
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Desposition","attachment; filename=demo.xls");
		return result;
	}
	
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,Alliance alliance) {
		ModelAndView modelView = new ModelAndView();
		String json=request.getParameter("submitData");
		Gson gson = new Gson();
		Map<String,String> map = new HashMap<String,String>();
		alliance = gson.fromJson(json,Alliance.class);
		if(alliance.getId()!=null&&!alliance.getId().equals(""))
			this.allianceManager.update(alliance);
		else
			this.allianceManager.save(alliance);
		ajaxPack("{state:true}",request,response);
		return null;
	}
	
	/**
	 * 根据id获取用户对象
	 **/
	public ModelAndView getId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		Alliance alliance=allianceManager.getById(id);
		if(alliance!=null){
			JSONObject obj = new JSONObject(alliance);
			ajaxPack(obj.toString(), request, response);
		}
		return null;
	}
	
	/**
	 * 登出
	 **/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		if(id.length()>0){
			id=id.substring(0,id.length()-1);
		}
		try {
			allianceManager.remove("DELETE FROM Alliance WHERE id in("+id+")");
			ajaxPack("{state:true}",request,response);
		} catch (RuntimeException e) {
			ajaxPack("{state:false}",request,response);
		}
		return null;
	}
}
